<?php
/**
* 
*/
import('ORG.Util.JSSDK');
include '';
class ScanAction extends Action
{
	public function index(){

   		$jssdk = new JSSDK("wx9d8f3d6a105a7b28", "9638441d52c7b712637ead74d0922401");
        $signPackage = $jssdk->GetSignPackage();
        //dump($signPackage);
        $history = M('search')->where(array('wid'=>$_COOKIE['wid']))->order('num desc')->limit(5)->select();
        $this->assign('history',$history);
        $this->assign('signpackage',$signPackage);
        $this->display();
	}
	public function result(){
   		$jssdk = new JSSDK("wx9d8f3d6a105a7b28", "9638441d52c7b712637ead74d0922401");
        $signPackage = $jssdk->GetSignPackage();
        //dump($signPackage);
        $this->assign('signpackage',$signPackage);
        $this->display();
	}
	public function isbmreturn(){
		@header("Content-type: text/html; charset=utf-8");
		$isbm = trim($_GET['keyword']);
		$data['wid'] = $_COOKIE['wid'];
		$data['keyword'] = $isbm;
		$data['num'] = 1;
		$ret = M('search')->where(array('keyword'=>$isbm))->find();
		if($ret){
			$num['num']=$ret['num']+1;
			$ret = M('search')->where(array('keyword'=>$isbm))->save($num);
		}else{
			M('search')->add($data);
		}
		//dump($isbm);die;
		// $url = 'https://api.douban.com/v2/book/search?q='.$isbm;
		// $content = file_get_contents($url);
		// $array = json_decode($content,true);
		$map['book_name']=array('like','%'.$isbm.'%');
		$data = M('book')->where($map)->select();
		//$data = $array['books'];
		//dump($data);
		$this->assign('book',$data);
		$this->display('booksearch');
		//$this->ajaxReturn($data);
	}
	public function booksearch(){
		$this->display();
	}
	public function isbmreturn_2(){
		@header("Content-type: text/html; charset=utf-8");
		$isbm = $_GET['isbm'];
		$isbm = substr($isbm,7);
		$id = $_GET['id'];
		//dump($isbm);die;
		//$id = $_COOKIE['wid'];
		//dump($_COOKIE['wid']);die;
		//$url = 'https://api.douban.com/v2/book/isbn/:'.$isbm;
		if($isbm!=''){
		$data = M('book')->where(array('book_isbm'=>$isbm))->find();
		}
		if($id!=''){
			$data = M('book')->where(array('id'=>$id))->find();
		}
		$map['cat'] = array('eq',$data['cat']);
		$map['id'] = array('neq',$data['id']);
		$xgbook = M('book')->where($map)->order('id desc')->limit(3)->select();
		//dump($data['cat']);
		//dump($xgbook);die;
		$data['wid'] = $_COOKIE['wid'];
		$data['bid'] = $data['id'];
		//dump($data);
		//$content = file_get_contents($url);
		//$data = json_decode($content,true);

		if($data==null){
			echo('<h1>没有找到该书</h1><br><h2>3秒后返回上一页面</h2>');
			echo('<script type="text/javascript">
setTimeout("jump()",3000);
 
function jump(){
    parent.history.back();
}
</script>');
			die;
		}else{
		//dump($data);
		//dump($xgbook);
		$this->assign('xgbook',$xgbook);
		$this->assign('data',$data);
		$this->display('isbmreturn');
		//$this->ajaxReturn($data);
		}
	}
	public function lend(){
		if(empty($_GET['bid'])||empty($_GET['wid'])){
			$this->error('管理员尚未添加此书，请联系管理员添加');die;
		}
		$res = M('lendlog')->where(array('bid'=>$_GET['bid'],'wid'=>$_GET['wid']))->find();
		//dump($res);die;
		if($res['status']=='1'){
			$this->error('您已经借过该书，不允许多本相同图书');die;
		}
		if($res){
			$data['lqr'] = $res['lqr'];
		}else{
		vendor("PHPQRcode.phpqrcode");
		$data['wid'] = $_GET['wid'];
		$data['bid'] = $_GET['bid'];
		$data['status'] = 0;
		$data['lendtime'] = time();
		$filename ='lend/'.time().'.png'; 
      	$data['info']='wid:'.$data['wid'].',bid:'.$data['bid']; 
     	QRcode::png($data['info'], $filename, 'L', 4,2,false);
      	$data['lqr'] = $filename;
		$ret = M('lendlog')->add($data);
		}
			$this->assign('data',$data);
			$this->display();
		}


		public function lendlog(){
			$wid = $_COOKIE['wid'];
			//dump($wid);
			$array = M('lendlog')->where(array('wid'=>$wid,'status'=>1))->order('id desc')->select();
			foreach ($array as $key => $value) {
				$array[$key]['book_name'] = M('book')->where(array('id'=>$value['bid']))->getField('book_name');
				$array[$key]['picture'] =  M('book')->where(array('id'=>$value['bid']))->getField('picture');
			}
			//dump($array);
			$this->assign('data',$array);
			$this->display();
		}
		public function givelog(){
			$wid = $_COOKIE['wid'];
			//dump($wid);
			$array = M('lendlog')->where(array('wid'=>$wid,'status'=>2))->order('id desc')->select();
			foreach ($array as $key => $value) {
				$array[$key]['book_name'] = M('book')->where(array('id'=>$value['bid']))->getField('book_name');
				$array[$key]['picture'] =  M('book')->where(array('id'=>$value['bid']))->getField('picture');
			}
			//dump($array);
			$this->assign('data',$array);
			$this->display();
		}
		public function home(){
			$sql = "select DISTINCT bid from wx_lendlog";   //热门图书
			$array = M()->query($sql);							
			//dump($array);
			foreach ($array as $key => $value) {
				$a = M('lendlog')->where(array('bid'=>$value['bid']))->select();
				$array[$key]['num'] = count($a);
			}
			$arr_ret = $this->array_sort($array,'num');
			foreach ($arr_ret as $key => $value) {
				//dump($value['bid']);
				//$book_info = M('book')->where('id')
				if($key<=5){
				$book_info[$key] = M('book')->where(array('id'=>$value['bid']))->find();
			}
			}
			$keyword = M('search')->order('num desc')->limit(1)->find();
			$map['book_name']=array('like','%'.$keyword['keyword'].'%');
			$data = M('book')->where($map)->select();
			$this->assign('data',$data);
			$this->assign('book_info',$book_info);
			$this->display();
		}
		public function cat(){
			$id = $_GET['id'];
			$data = M('book')->where(array('cat'=>$id))->select();
			$this->assign('data',$data);
			$this->display();
		}
		function array_sort($arr,$keys,$type='desc'){ 
			$keysvalue= $new_array= array(); 
			foreach($arr as $k=>$v){ 
			$keysvalue[$k] = $v[$keys]; 
			} 
			if($type== 'asc'){ 
			asort($keysvalue); 
			}else{ 
			arsort($keysvalue); 
			} 
			reset($keysvalue); 
			foreach($keysvalue as $k=>$v){ 
			$new_array[$k] = $arr[$k]; 
			} 
			return $new_array; 
			} 
}
?>